<template>
  <el-table :data="dataList" v-loading="dataListLoading" stripe>
    <el-table-column prop="id" :label="i18n('common.id')" width="70">
    </el-table-column>

    <el-table-column :label="i18n('share.share_name')" min-width="200">
      <template v-slot="scope">
        <dl class="table_dl" v-if="1 == scope.row.role_id && scope.row.user">
          <dt>
            <el-avatar :size="45" :src="scope.row.user.avatar" />
          </dt>
          <dd class="table_dl_dd_all_30">
            {{ scope.row.user.nickname }}
          </dd>
          <dd class="table_dl_dd_all_16_gray">
            <span v-if="scope.row.user.organization">
              {{ scope.row.user.organization.title }}
            </span>
          </dd>
        </dl>
        <dl class="table_dl" v-if="2 == scope.row.role_id && scope.row.contact">
          <dt>
            <el-avatar :size="45" :src="scope.row.contact.avatar" />
          </dt>
          <dd class="table_dl_dd_all_30">
            {{ scope.row.contact.name }}
          </dd>
          <dd class="table_dl_dd_all_16_gray">
            <span v-if="scope.row.contact.client">
              {{ scope.row.contact.client.name }}
            </span>
          </dd>
        </dl>
      </template>
    </el-table-column>

    <el-table-column :label="i18n('share.module')" min-width="200">
      <template v-slot="scope">
        <dl class="table_dl" v-if="scope.row.product">
          <dt>
            <el-image class="cx-list-image" :src="scope.row.product.picture" loading="lazy" lazy>
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
          </dt>
          <dd class="table_dl_dd_all_30">
            {{ scope.row.product.title  }}
          </dd>
          <dd class="table_dl_dd_all_16_gray">
            <el-link @click="dialogVisible = true;loadQrcodeUrl(scope.row.path)" :underline="false">
              <span>点击查看</span>
              <el-icon class="mr5"><ArrowRight /></el-icon>
            </el-link>
          </dd>
        </dl>
      </template>
    </el-table-column>

    <el-table-column :label="$t('browse.name_type')" width="100">
      <template v-slot="scope">
        {{ scope.row.role_text }}
      </template>
    </el-table-column>

    <el-table-column :label="i18n('share.create_time')" width="160">
      <template v-slot="scope">
        <dl class="table_dl">
          <dd class="table_dl_dd_all_30">
            {{ scope.row.create_time_text }}
          </dd>
          <dd class="table_dl_dd_all_16_gray">
            {{ scope.row.create_time }}
          </dd>
        </dl>
      </template>
    </el-table-column>
  </el-table>
  <div class="admin_table_main_pagination">
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
      background
      layout="prev, pager, next,jumper,total">
    </el-pagination>
  </div>

  <el-dialog v-model="dialogVisible" title="在小程序上查看" width="300" center>
    <div class="text-center mt20">
      <el-image class="qrcode" :src="qrcode_url"/>
    </div>
  </el-dialog>
</template>

<script setup name="share-product-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/share/product')

  const dialogVisible = ref(false)
  const qrcode_url = ref('')

  const dataForm = reactive({
    category_id: '',
  })

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadQrcodeUrl = (path) => {
    $http({
      url: $http.adornUrl('/business/qrcode/mini/data'),
      method: 'get',
      params: $http.adornParams({
        path: path,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        qrcode_url.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    if(query.category_id) {
      dataForm.category_id = parseInt(query.category_id)
    }

    getDataList(model)
  })
</script>

<style scoped lang="scss">
  .qrcode {
    width: 10vw;
    height: 10vw;
  }
</style>
